//
//  GiftListDataSource.m
//  tcpoctest
//
//  Created by gump on 5/22/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "GiftListDataSource.h"
#import "giftlistDataInfo.h"
#import "LocalPathDataSource.h"
#import "giftListDataBase.h"

@implementation GiftListDataSource

-(id)init
{
    self = [super init];
    
    if (self != nil) {
        GLShowArr = [[NSMutableArray alloc] init];
        GLDic = [[NSMutableDictionary alloc] init];
        GLImageDic = [[NSMutableDictionary alloc] init];
        GLDownImageDic = [[NSMutableDictionary alloc] init];
        
        pthread_mutex_init(&mutexGL, 0);
        pthread_mutex_init(&mutexGLDown, 0);
    }
    
    return self;
}

-(void)AddGiftList:(int)iid withGiftInfo:(giftlistDataInfo *)giftinfo
{
    pthread_mutex_lock(&mutexGL);
    
    if (giftinfo != nil ) {
        if (![GLShowArr containsObject:[NSNumber numberWithInt:iid]]) {
            [GLShowArr addObject:[NSNumber numberWithInt:iid]];
            
            [GLDic setObject:giftinfo forKey:[NSNumber numberWithInt:iid]];
        }
    }
    
    pthread_mutex_unlock(&mutexGL);
}

-(int)GetGiftIDByRow:(int)irow
{
    int iid = 0;
    pthread_mutex_lock(&mutexGL);
    
    if (irow >= 0 && irow < [GLShowArr count]) {
        NSNumber *num = [GLShowArr objectAtIndex:irow];
        iid = [num intValue];
    }
    
    pthread_mutex_unlock(&mutexGL);
    
    return iid;
}

-(void)delAllList
{
    pthread_mutex_lock(&mutexGL);
    
    [GLShowArr removeAllObjects];
    [GLDic removeAllObjects];
    
    pthread_mutex_unlock(&mutexGL);
}

-(int)GetShowArrCount
{
    int icount = 0;
    pthread_mutex_lock(&mutexGL);
    
    icount = GLShowArr.count;
    
    pthread_mutex_unlock(&mutexGL);
    
    return icount;
}

-(NSString *)GetNameByRow:(int)irow
{
    NSString *strname = @"";
    
    pthread_mutex_lock(&mutexGL);
    
    if (irow >= 0 && irow < GLShowArr.count) {
        NSNumber *num = [GLShowArr objectAtIndex:irow];
        int igiftid = [num intValue];
        giftlistDataInfo *ginfo = [GLDic objectForKey:[NSNumber numberWithInt:igiftid]];
        
        if (ginfo == nil) {
            strname = @"";
        }
        else
        {
            strname = ginfo.name;
        }
    }
    
    pthread_mutex_unlock(&mutexGL);
    
    return strname;
}

-(NSString *)GetUrlByRow:(int)irow
{
    NSString *strname = @"";
    
    pthread_mutex_lock(&mutexGL);
    
    if (irow >= 0 && irow < GLShowArr.count) {
        NSNumber *num = [GLShowArr objectAtIndex:irow];
        int igiftid = [num intValue];
        giftlistDataInfo *ginfo = [GLDic objectForKey:[NSNumber numberWithInt:igiftid]];
        
        if (ginfo == nil) {
            strname = @"";
        }
        else
        {
            strname = ginfo.url;
        }
    }
    
    pthread_mutex_unlock(&mutexGL);
    
    return strname;
}

-(void)delImageDic
{
    pthread_mutex_lock(&mutexGL);
    
    [GLImageDic removeAllObjects];
    
    pthread_mutex_unlock(&mutexGL);
}

-(void)InsertImageDicByID:(int)iid withPath:(NSString *)strpath
{
    pthread_mutex_lock(&mutexGL);
    
    [GLImageDic setObject:strpath forKey:[NSNumber numberWithInt:iid]];
    
    pthread_mutex_unlock(&mutexGL);
}

-(NSString *)searchPathByRow:(int)irow
{
    NSString *path = @"";
    
    pthread_mutex_lock(&mutexGL);
    
    if (irow >= 0 && irow < GLShowArr.count) {
        NSNumber *num = [GLShowArr objectAtIndex:irow];
        int igiftid = [num intValue];
        
        path = [GLImageDic objectForKey:[NSNumber numberWithInt:igiftid]];
        
        if (path == nil) {
            path = @"";
            
            giftlistDataInfo *ginfo = [GLDic objectForKey:[NSNumber numberWithInt:igiftid]];
            if (ginfo != nil) {
                
                pthread_mutex_lock(&mutexGLDown);
                
                if ([GLDownImageDic objectForKey:
                     [NSNumber numberWithInt:ginfo.giftid]] == nil) {
                    [GLDownImageDic setObject:ginfo.url
                                       forKey:[NSNumber numberWithInt:ginfo.giftid]];
                    
                    pthread_mutex_unlock(&mutexGLDown);
                    
                    [[DataControl shareControl].LCDS addLogicCommand:LC_GIFT_DOWNIMAGE];
                }
                else
                {
                    pthread_mutex_unlock(&mutexGLDown);
                }
                
            }
        }
    }
    
    pthread_mutex_unlock(&mutexGL);
    
    return path;
}

-(void)DownLoadImageArr
{
    pthread_mutex_lock(&mutexGLDown);
    
    NSArray *urlarr = [GLDownImageDic allKeys];
    for (NSNumber *imagenum in urlarr) {
        
        NSString *url = [GLDownImageDic objectForKey:imagenum];
        
        NSData *imagedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
        
        if (imagedata != nil && [imagedata length] > 0) {
            
            NSString *urlpath = @"";
            NSString *strname = getFileNameWithDate(FT_GIFT);
            
            int inum = [imagenum intValue];
            
            giftlistDataInfo *ginfo = [GLDic objectForKey:[NSNumber numberWithInt:inum]];
            
            if (ginfo == nil) {
                continue;
            }
            
            strname = [NSString stringWithFormat:@"%@%d",strname,ginfo.giftid];
            
            urlpath =[[[DataControl shareControl].localpathds giftPath] stringByAppendingPathComponent:strname];
            
            BOOL bsuccess = [imagedata writeToFile:urlpath atomically:YES];
            
            do {
                
                if (!bsuccess) {
                    break;
                }
                
                [[DataBaseCtrol shareControl].giftListImageDB insertInfoByID:inum withPath:urlpath];
                [GLImageDic setObject:urlpath forKey:[NSNumber numberWithInt:inum]];
                
                
                
            } while (false);
        }
        
    }
    
    [GLDownImageDic removeAllObjects];
    
    pthread_mutex_unlock(&mutexGLDown);
    
    [[DataControl shareControl].UICommendDS addUICommand:UC_GIFT_REFRESHLIST];
}

@end
